<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">库存首页</el-breadcrumb-item>
      <el-breadcrumb-item>物品基础库</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.name" clearable @clear="getList">
            <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-select v-model="dictionary_id" placeholder="请选择物品类别一类" @change="getListByDictionary(dictionary_id)">
            <el-option
              v-for="item in dictionarys"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="category_id" placeholder="请选择物品类别二级类" @change="getListByCategory(category_id)">
            <el-option
              v-for="item in categorys"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button type="primary" @click="addDialogVisible = true">新增</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="tableData"
        style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="一类名称"
          width="180">
          <template slot-scope="scope">
            {{scope.row.dictionary.name}}
          </template>
        </el-table-column>
        <el-table-column
          label="二类名称">
          <template slot-scope="scope">{{scope.row.category.name}}</template>
        </el-table-column>
        <el-table-column label="数量" prop="amount">

        </el-table-column>
        <el-table-column label="备注" prop="memo">

        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current_page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <el-dialog title="新增" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主体 -->
      <el-form
        :model="addForm"
        ref="addFormRef"
        :rules="addFormRules"
        label-width="auto"
      >
        <el-form-item label="物品类别一类" prop="dictionary_id">
          <el-select v-model="addForm.dictionary_id" placeholder="请选择物品类别一类" @change="dialogGetListByDictionary($event.target.value)">
            <el-option
              v-for="item in dictionarys"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品类别二级类" prop="category_id">
          <el-select v-model="addForm.category_id" placeholder="请选择物品类别二级类">
            <el-option
              v-for="item in addForm.categorys"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="amount">
          <el-input v-model="addForm.amount" type="number"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="addForm.memo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体 -->
      <el-form
        :model="editForm"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="一类名称" prop="dictionary_id">
          <el-select v-model="editForm.dictionary_id" placeholder="请选择一类类型">
            <el-option
              v-for="item in dictionarysEdit"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二类类型" prop="category_id">
          <el-select v-model="editForm.category_id" placeholder="请选择人员类型">
            <el-option
              v-for="item in categorysEdit"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Base",
  data() {
    return {
      editDialogVisible:false,
      total: 0,
      tableData: [],
      queryInfo: {
        category_id: 0,
        dictionary_id: 0,
        current_page: 1,
        pagesize: 10
      },
      dictionary_id: '',
      dictionarys: [],
      dictionarysEdit: [],
      categorys: [],
      categorysEdit: [],
      category_id: '',
      addDialogVisible: false,
      editForm: {},
      addForm: {
        category_id: '',
        categorys: [],
        amount: 0,
        memo:''
      },
      addFormRules: {
        dictionary_id: [
          {required:true,message:'请选择一类', trigger: 'blur'}
        ],
        category_id:[
          {required:true,message:'请选择二类', trigger: 'blur'}
        ],
        amount:[
          {required:true,message:'请输入数量', trigger: 'blur'},
          {min: 1, message: '值必须大于0', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getDictionarys()
    this.getList();
  },
  methods: {
    removeById(id) {

    },
    edit() {

    },
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 编辑信息
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('goodsShow/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询信息失败！')
      }
      this.dictionarysEdit = res.data
      this.editDialogVisible = true
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.current_page = newSize
      this.getList()
    },
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    async getDictionarys() {
      this.queryInfo.type_id = 1;
      const { data: res } = await this.$http.get('dictionaryList', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取物品类别一类失败！')
      }
      this.dictionarys = res.data.data
    },
    async getCategorys(id) {
      const { data: res } = await this.$http.get('categoryList/'+id)
      this.categorys = res.data
      if (res.meta.status !== 200) {
        this.category_id=''
        return this.$message.error('获取物品类别二级类失败！')
      }

    },
    async dialogGetCategorys(id) {
      const { data: res } = await this.$http.get('categoryList/'+id)
      this.addForm.categorys = res.data
      this.addForm.category_id=''
      if (res.meta.status !== 200) {
        return this.$message.error('获取物品类别二级类失败！')
      }
    },
    async getList() {
      const { data: res } = await this.$http.get('goodsList', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取列表失败！')
      }
      this.tableData = res.data.data
      this.total = res.data.total
    },
    add() {
// 提交请求前，表单预验证
      this.$refs.addFormRef.validate(async valid => {
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$http.post('goodsAdd', this.addForm)
        if (res.meta.status !== 200) {
          return this.$message.error('添加失败！')
        }
        this.$message.success('添加成功！')
        // 隐藏添加用户对话框
        this.addDialogVisible = false
        await this.getList()
      })
    },
    getListByDictionary(id) {
      this.getCategorys(id);
    },
    dialogGetListByDictionary(id) {
      console.log(id)
      this.dialogGetCategorys(id);
    },
    getListByCategory(id) {

    }
  }
};
</script>

<style scoped>

</style>
